package com.itao.security;

import java.io.Serializable;
import java.util.Collection;

import org.springframework.security.access.PermissionEvaluator;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;


public class OpcnetPermissionEvaluator implements PermissionEvaluator {

    public boolean hasPermission(Authentication authentication, Object target) {
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        for (GrantedAuthority authority : authorities) {
            if (target instanceof String) {
                
                String targetAce = (String) target;
                    if (authority.getAuthority().equalsIgnoreCase(targetAce)) {
                        return true;
                    }
            }
        }
        return false;
    }

    @Override
    public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
        return hasPermission(authentication, targetDomainObject);
    }

    @Override
    public boolean hasPermission(Authentication authentication, Serializable targetId, String targetType,
            Object permission) {
        return hasPermission(authentication, targetId);
    }

}
